package com.gcsoftware.web.controller.admin;

import com.gcsoftware.utils.MapUtils;
import com.gcsoftware.utils.SysConst;
import com.gcsoftware.utils.log.LogUtils;
import com.gcsoftware.web.beans.*;
import com.gcsoftware.web.beans.request.GetInterfReq;
import com.gcsoftware.web.beans.response.T_GW_INTERFS_RSP;
import com.gcsoftware.web.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qs on 2018/3/17.
 */
@Controller
@RequestMapping("/verify")
public class VerifyController {

    @Autowired
    private  T_COM_SYS_DICT_SERVICE dictService;

    @Autowired
    private T_COM_SYS_USER_SERVICE userService;

    @Autowired
    private T_GW_APPS_SERVICE appService;

    @Autowired
    private T_GW_INTERF_SERVICE interfService;

    @Autowired
    private T_GW_VERIFY_SERVICE service;

    @Autowired
    private T_GW_CHANGES_SERVICE changeService;

    @Autowired
    private T_GW_COMPANY_SERVICE companyService;

    @Autowired
    private T_COM_SYS_LOGSERVICE logService;

    @Autowired
    private T_GW_ROLE_SERVICE roleService;

    @RequestMapping("")
    public String verify(Model model) {
        model.addAttribute("verifyTypes",dictService.queryForList(SysConst.DICT_GRP_VERIFY_TYPES));
        model.addAttribute("verifyStatus",dictService.queryForList(SysConst.DICT_GRP_VERIFY_STATUS));
        return "admin/verify";
    }

    @ResponseBody
    @RequestMapping(value = "/list" , method = RequestMethod.POST)
    public List<T_GW_VERIFY> queryForList(@RequestParam(value = "c_VERIFY_TYPE" , required = false) String c_VERIFY_TYPE,@RequestParam(value = "c_VERIFY_STATUS" , required = false) String c_VERIFY_STATUS) {
        return service.queryForList(c_VERIFY_TYPE,c_VERIFY_STATUS);
    }

    @ResponseBody
    @RequestMapping(value = "/u/{c_VERIFY_ID}" , method = RequestMethod.POST)
    public MapUtils userVerify(@PathVariable(value = "c_VERIFY_ID") String c_VERIFY_ID) {
        T_GW_VERIFY verify = service.getById(c_VERIFY_ID);
        if(null != verify) {
            T_COM_SYS_USER userInfo = userService.get(verify.getC_SUBMIT_USER(),null,null);
            if (null != userInfo) {
                return MapUtils.ok().put("userInfo" , userInfo);
            }
        }
        return null;
    }
    @ResponseBody
    @RequestMapping(value = "/com/{c_VERIFY_ID}" , method = RequestMethod.POST)
    public MapUtils comVerify(@PathVariable(value = "c_VERIFY_ID") String c_VERIFY_ID) {
        T_GW_VERIFY verify = service.getById(c_VERIFY_ID);
        if(null != verify) {
            T_GW_COMPANY company = companyService.get(verify.getC_COMPANY_ID(),null);
            if (null != company) {
                return MapUtils.ok().put("companyInfo" , company);
            }
        }
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/appreg/{c_VERIFY_ID}" , method = RequestMethod.POST)
    public MapUtils appRegVerify(@PathVariable(value = "c_VERIFY_ID") String c_VERIFY_ID) {
        T_GW_VERIFY verify = service.getById(c_VERIFY_ID);
        if(null != verify) {
            T_GW_APPS apps = appService.getById(verify.getC_APP_ID());
            if (null != apps) {
                List<T_GW_APP_IPS> ips = appService.queryForIpList(apps.getC_APP_ID());
                if(CollectionUtils.isNotEmpty(ips)) {
                    apps.setAppIps(ips);
                }
                return MapUtils.ok().put("appInfo" , apps);
            }
        }
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/{c_VERIFY_ID}/{rst}" , method = RequestMethod.POST)
    public MapUtils doVerify(@PathVariable(value = "c_VERIFY_ID") String c_VERIFY_ID,@PathVariable(value = "rst") String rst,
                             @RequestParam(value = "c_APP_ID" , required = false) String c_APP_ID,
                             @RequestParam(value = "c_ROLE_ID" , required = false) String c_ROLE_ID) {
        rst = "Y".equals(rst.toUpperCase()) ? SysConst.VERIFY_STATUS_APPROVED : SysConst.VERIFY_STATUS_NOT_APPROVED;
        T_GW_VERIFY verify = service.getById(c_VERIFY_ID);
        if ( 1 == service.updateVerifyStatus(c_VERIFY_ID,rst) && SysConst.VERIFY_STATUS_APPROVED.equals(rst)) {
            if (SysConst.VERIFY_TYPE_USER_REGISTER.equals(verify.getC_VERIFY_TYPE())) {
                userService.updateUserStatus(verify.getC_SUBMIT_USER(), SysConst.DICT_USER_STATUS_NORMAL);
            }else if (SysConst.VERIFY_TYPE_COMPANY_REGISTER.equals(verify.getC_VERIFY_TYPE())) {
                companyService.updateStatus(verify.getC_COMPANY_ID(),SysConst.DICT_COMPANY_STATUS_NORMAL);
            }
            else if (SysConst.VERIFY_TYPE_APP_REGISTER.equals(verify.getC_VERIFY_TYPE())) {
                appService.updateAppStatus(verify.getC_APP_ID(),SysConst.DICT_APP_STATUS_PUBLISH);
                appService.updateAppPwd(verify.getC_APP_ID());
                if (StringUtils.isNotBlank(c_APP_ID) && StringUtils.isNotBlank(c_ROLE_ID)) {
                    appService.updateAppRole(c_APP_ID,c_ROLE_ID);
                }
            } else if (SysConst.VERIFY_TYPE_APP_OFFLINE.equals(verify.getC_VERIFY_TYPE())) {
                appService.updateAppStatus(verify.getC_APP_ID(),SysConst.DICT_APP_STATUS_OFFLINE);
            } else if (SysConst.VERIFY_TYPE_APP_CHANGED.equals(verify.getC_VERIFY_TYPE())) {
                List<T_GW_CHANGES> cgs = changeService.queryForList(verify.getC_VERIFY_ID());
                T_GW_APPS appInfo = appService.getById(verify.getC_APP_ID());
                appInfo = changeService.getAppFromChange(appInfo,cgs);
                appInfo.setC_APP_ID(verify.getC_APP_ID());

                List<T_GW_APP_IPS> ips = changeService.getIpsFromChange(verify.getC_APP_ID(),cgs);
                appService.appChanged(appInfo,ips);

                appService.updateAppRole(c_APP_ID,c_ROLE_ID);
            }else if (SysConst.VERIFY_TYPE_INTERF_REGISTER.equals(verify.getC_VERIFY_TYPE())) {
                GetInterfReq req = new GetInterfReq();
                req.setC_INTERF_ID(verify.getC_INTERF_ID());
                req.setC_INTERF_STATUS(SysConst.DICT_INTERF_STATUS_PUBLISH);
                interfService.updateInterf(req);
                //appService.updateAppPwd(verify.getC_APP_ID());
            } else if (SysConst.VERIFY_TYPE_INTERF_CHANGED.equals(verify.getC_VERIFY_TYPE())) {

            }else if (SysConst.VERIFY_TYPE_INTERF_OFFLINE.equals(verify.getC_VERIFY_TYPE())) {
                GetInterfReq req = new GetInterfReq();
                req.setC_INTERF_ID(verify.getC_INTERF_ID());
                req.setC_INTERF_STATUS(SysConst.DICT_INTERF_STATUS_OFFLINE);
                interfService.updateInterf(req);
            }

        }

        logService.insertSysLog(LogUtils.LOG_TYPE_OPERATE,"审核" + SysConst.getDescriptionByVerifyType(verify.getC_VERIFY_TYPE()) + (SysConst.VERIFY_STATUS_APPROVED.equals(rst) ? "通过":"不通过"),"OPERATE_STATUS_SUCCESS");
        return MapUtils.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/app/{c_VERIFY_ID}" , method = RequestMethod.POST)
    public MapUtils appVerify(@PathVariable(value = "c_VERIFY_ID") String c_VERIFY_ID) {
        T_GW_VERIFY verify = service.getById(c_VERIFY_ID);
        if(null != verify) {
            T_GW_APPS apps = appService.getById(verify.getC_APP_ID());

            if (null != apps) {
                List<T_GW_APP_IPS> ips = appService.queryForIpList(apps.getC_APP_ID());
                if(CollectionUtils.isNotEmpty(ips)) {
                    apps.setAppIps(ips);
                }
                Map<String , Object> map = new HashMap<>();
                map.put("appInfo" , apps);

                if (SysConst.VERIFY_TYPE_APP_OFFLINE.equals(verify.getC_VERIFY_TYPE())) {
                    map.put("appOffLineRemark" , verify.getC_VERIFY_REMARK());
                }else {
                    if (SysConst.VERIFY_TYPE_APP_CHANGED.equals(verify.getC_VERIFY_TYPE())) {
                        //应用变更，装载变更IP
                        List<T_GW_CHANGES> changes = changeService.queryForList(verify.getC_VERIFY_ID());
                        List<T_GW_APP_IPS> changeIps = changeService.getIpsFromChange(verify.getC_APP_ID(),changes);
                        map.put("changeIps" , changeIps);
                    }
                    map.put("roles" , roleService.queryForList(SysConst.SYS_ROLE_TYPE_COMPANY,null));
                }
                return MapUtils.ok(map);
            }
        }
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/interf/{c_VERIFY_ID}" , method = RequestMethod.POST)
    public MapUtils interfVerify(@PathVariable(value = "c_VERIFY_ID") String c_VERIFY_ID,
                                 @RequestParam(value = "c_VERIFY_TYPE" ,required = false)String c_VERIFY_TYPE) {
        T_GW_VERIFY verify = service.getById(c_VERIFY_ID);
        if(null != verify) {
            T_GW_INTERFS_RSP rsp = (T_GW_INTERFS_RSP) interfService.getForRsp(verify.getC_INTERF_ID());

            if (null != rsp) {

                List<T_GW_INTERF_PARAMS> ips = interfService.queryInterfParams(verify.getC_INTERF_ID());
                if(CollectionUtils.isNotEmpty(ips)) {
                    rsp.setIparams(ips);
                }
                Map<String , Object> map = new HashMap<>();
                map.put("interfsInfo" , rsp);

                if (SysConst.VERIFY_TYPE_INTERF_OFFLINE.equals(verify.getC_VERIFY_TYPE())) {
                    map.put("interfOffLineRemark" , verify.getC_VERIFY_REMARK());
                } else if (SysConst.VERIFY_TYPE_INTERF_CHANGED.equals(verify.getC_VERIFY_TYPE())) {
                    map.put("changedServer" , changeService.getChangeIntefsForService(verify.getC_VERIFY_ID()));
                }
                return MapUtils.ok(map);
            }
        }
        return null;
    }
}
